<template>
  <div>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="日期">
                <a-range-picker :allowClear="true" ref="dateRangePicker" @change="dateRangePickerChange"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button
                  type="primary"
                  @click="refreshList"
                >查询</a-button>
                <a-button style="margin-left: 8px" @click="handleReset">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <a-tabs default-active-key="1">
        <a-tab-pane key="1" tab="待返现">
          <s-table
            ref="cashBackTable"
            size="default"
            rowKey="id"
            :columns="cashBackColumns"
            :alert="cashBackOptions.alert"
            :rowSelection="cashBackOptions.rowSelection"
            :data="loadCashBackData"
            showPagination="auto"
          >
            <span slot="action" slot-scope="text, record">
              <a @click="handleConfirmCashBack([record.id])">确认返现</a>
            </span>

            <span slot="footer">
              <a-button
                @click="handleConfirmCashBack(cashBackSelectedRowKeys)"
                :disabled="cashBackSelectedRowKeys.length === 0"
                type="primary"
              >批量确认</a-button>
            </span>
          </s-table>
        </a-tab-pane>
        <a-tab-pane key="2" tab="已返现" force-render>
          <s-table
            ref="alreadyCashBackTable"
            size="default"
            rowKey="id"
            :columns="alreadyCashBackColumns"
            :scroll="{ x: 1300 }"
            :data="loadAlreadyCashBackData"
            showPagination="auto"
          >
          </s-table>
        </a-tab-pane>
      </a-tabs>
    </a-card>
  </div>
</template>

<script>
  import { STable } from '@/components'
  import PartnersForm from '@/views/proxy/partner/Form'
  import { approvalOfDailyAwards, getDailyRewardList } from '@/api/finance'
  import { formatAmount } from '@/utils/util'

  export default {
    name: 'List',
    components: {
      PartnersForm,
      STable
    },
    data () {
      return {
        // 查询参数
        queryParam: {},
        // 待返现表头
        cashBackColumns: [
          {
            title: '日期',
            dataIndex: 'award_date'
          },
          {
            title: '代理名称',
            dataIndex: 'agent.name'
          },
          {
            title: '代理手机号',
            dataIndex: 'agent.phone'
          },
          {
            title: '满足条件',
            customRender: item => '满足' + formatAmount(item.threshold_amount) + '返' + formatAmount(item.reward)
          },
          {
            title: '返现金额',
            dataIndex: 'reward',
            needTotal: true,
            customRender: reward => formatAmount(reward)
          },
          {
            title: '操作',
            width: '150px',
            fixed: 'right',
            scopedSlots: { customRender: 'action' }
          }
        ],
        // 待返现 选中 配置
        cashBackOptions: {
          alert: {
            show: true,
            clear: () => {
              this.cashBackSelectedRowKeys = []
            }
          },
          rowSelection: {
            selectedRowKeys: this.cashBackSelectedRowKeys,
            onChange: this.onCashBackSelectedRowKeysChange
          }
        },
        cashBackSelectedRowKeys: [],
        // 已返现表头
        alreadyCashBackColumns: [
          {
            title: '日期',
            dataIndex: 'award_date'
          },
          {
            title: '代理名称',
            dataIndex: 'agent.name'
          },
          {
            title: '代理手机号',
            dataIndex: 'agent.phone'
          },
          {
            title: '满足条件',
            customRender: item => '满足' + formatAmount(item.threshold_amount) + '返' + formatAmount(item.reward)
          },
          {
            title: '返现金额',
            customRender: item => formatAmount(item.reward)
          },
          {
            title: '备注',
            dataIndex: 'remark'
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadCashBackData: parameter => {
          return getDailyRewardList(Object.assign(parameter, this.queryParam))
        },
        loadAlreadyCashBackData: parameter => {
          parameter.status = 2 // 已通过
          return getDailyRewardList(Object.assign(parameter, this.queryParam))
        }
      }
    },
    created () {

    },
    methods: {
      handleReset () {
        this.queryParam = {}
        this.$refs.datePicker.$refs.picker.clearSelection(new MouseEvent('click'))
      },
      dateRangePickerChange (moment, date) {
        this.queryParam.begin_date = date[0]
        this.queryParam.end_date = date[1]
      },
      handleConfirmCashBack (recordIds) {
        const _this = this
        this.$confirm({
          centered: true,
          keyboard: false,
          title: '确认要给代理商结算奖励吗？',
          content: h => (< a-textarea placeholder = "请填写备注信息，选填..." id = "remark" > < /a-textarea>),
          onOk () {
            return new Promise((resolve, reject) => {
              const remark = document.getElementById('remark').value
              approvalOfDailyAwards({
                remark: remark,
                ids: recordIds
              })
                .then(res => {
                  _this.$message.success('操作成功!')
                  resolve()
                })
                .catch(e => reject(e))
                .finally(() => _this.refreshList())
            })
          },
          onCancel () {
          }
        })
      },
      onCashBackSelectedRowKeysChange (cashBackSelectedRowKeys) {
        this.cashBackSelectedRowKeys = cashBackSelectedRowKeys
      },
      refreshList () {
        this.cashBackSelectedRowKeys = []
        this.$refs.cashBackTable.refresh(true)
        this.$refs.alreadyCashBackTable.refresh(true)
      }
    }
  }
</script>
